/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.baatar.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Ba
 */
public class XMLUtil {

    public static String readProperty(InputStream inputStream, String tagName, String attributeName) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputStream);

        NodeList listOfTags = doc.getElementsByTagName(tagName);

        if (listOfTags.getLength() > 0) {
            Node node = listOfTags.item(0);
            Element element = (Element) node;
            return element.getAttribute(attributeName);
        }
        return null;
    }

    public static ArrayList<String> readProperties(InputStream inputStream, String tagName, String attributeName) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputStream);

        NodeList listOfTags = doc.getElementsByTagName(tagName);

        ArrayList<String> res = new ArrayList<String>();

        for (int i = 0; i < listOfTags.getLength(); i++) {
            Node node = listOfTags.item(i);
            Element element = (Element) node;
            res.add(element.getAttribute(attributeName));
        }

        return res;
    }

    public static String readContent(InputStream inputStream, String tagName) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputStream);

        NodeList listOfTags = doc.getElementsByTagName(tagName);

        if (listOfTags.getLength() > 0) {
            Node node = listOfTags.item(0);
            Element element = (Element) node;
            return element.getTextContent();
        }
        return null;
    }

    public static ArrayList<String> readContents(InputStream inputStream, String tagName) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputStream);

        NodeList listOfTags = doc.getElementsByTagName(tagName);

        ArrayList<String> res = new ArrayList<String>();

        for (int i = 0; i < listOfTags.getLength(); i++) {
            Node node = listOfTags.item(i);
            Element element = (Element) node;
            res.add(element.getTextContent());
        }

        return res;
    }

    public static String readCharacterData(InputStream inputStream, String tagName) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputStream);

        NodeList listOfTags = doc.getElementsByTagName(tagName);

        if (listOfTags.getLength() > 0) {
            Node node = listOfTags.item(0);
            Element element = (Element) node;
            Node child = element.getFirstChild();
            return ((CharacterData) child).getData();
        }
        return null;
    }

    public static ArrayList<String> readCharacterDatum(InputStream inputStream, String tagName) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(inputStream);

        NodeList listOfTags = doc.getElementsByTagName(tagName);

        ArrayList<String> res = new ArrayList<String>();

        for (int i = 0; i < listOfTags.getLength(); i++) {
            Node node = listOfTags.item(i);
            Element element = (Element) node;
            Node child = element.getFirstChild();
            res.add(((CharacterData) child).getData());
        }

        return res;
    }

    public static String readProperty(String fileName, String tagName, String attributeName) throws ParserConfigurationException, SAXException, IOException {
        FileInputStream fis = new FileInputStream(fileName);
        try {
            return readProperty(fis, tagName, attributeName);
        } finally {
            fis.close();
        }
    }

    public static ArrayList<String> readProperties(String fileName, String tagName, String attributeName) throws ParserConfigurationException, SAXException, IOException {
        FileInputStream fis = new FileInputStream(fileName);
        try {
            return readProperties(fis, tagName, attributeName);
        } finally {
            fis.close();
        }
    }

    public static String readContent(String fileName, String tagName) throws ParserConfigurationException, SAXException, IOException {
        FileInputStream fis = new FileInputStream(fileName);
        try {
            return readContent(fis, tagName);
        } finally {
            fis.close();
        }
    }

    public static ArrayList<String> readContents(String fileName, String tagName) throws ParserConfigurationException, SAXException, IOException {
        FileInputStream fis = new FileInputStream(fileName);
        try {
            return readContents(fis, tagName);
        } finally {
            fis.close();
        }
    }

    public static String readCharacterData(String fileName, String tagName) throws ParserConfigurationException, SAXException, IOException {
        FileInputStream fis = new FileInputStream(fileName);
        try {
            return readCharacterData(fis, tagName);
        } finally {
            fis.close();
        }
    }

    public static ArrayList<String> readCharacterDatum(String fileName, String tagName) throws ParserConfigurationException, SAXException, IOException {
        FileInputStream fis = new FileInputStream(fileName);
        try {
            return readCharacterDatum(fis, tagName);
        } finally {
            fis.close();
        }
    }

}
